﻿using Microsoft.AspNetCore.Authorization;
using PetaPoco;
using PmSoft;
using PmSoft.Caching;
using System.ComponentModel;
using static Modules.Project.ProjectConstants;

namespace Modules.Project
{
    /// <summary>
    /// 项目基础信息表
    ///</summary>
    [TableName("project")]
    [Description("项目基础信息表")]
    [PrimaryKey("project_id", AutoIncrement = true)]
    [CacheSetting(true, PropertyNamesOfArea = "ManagerId,LumpContractorCorpId,BuildCorpId")]
    [Authorize(Roles = "Manager")]
    public class ProjectInfo : IDelEntity
    {
        public ProjectInfo()
        {
        }

        /// <summary>
        /// 新增项目
        /// </summary>
        /// <param name="prjStatus"></param>
        /// <param name="auditStatus"></param>
        /// <param name="createManagerId"></param>
        public ProjectInfo(项目状态 prjStatus, 项目审核状态 auditStatus, int createManagerId, 项目数据来源 dataSource)
        {
            this.PrjStatus = prjStatus;
            this.AuditStatus = auditStatus;
            this.CreateManagerId = createManagerId;
            this.CreateTime = DateTime.Now;
            this.DataSource = dataSource;
            this.NeedCompletData = dataSource != 项目数据来源.项目注册;
        }

        /// <summary>
        /// 项目ID
        ///</summary>
        [Column("project_id")]
        public int ProjectId { get; set; }

        /// <summary>
        /// 行业主管部门
        ///</summary>
        [Column("industry_type")]
        public 行业主管部门 IndustryType { get; set; }

        /// <summary>
        /// 行业主管部门项目编号/住建：西藏自治区建筑市场监管平台项目编号
        ///</summary>
        [Column("industry_prj_num")]
        public string IndustryPrjNum { get; set; }

        /// <summary>
        /// 项目名称
        ///</summary>
        [Column("name")]
        public string Name { get; set; }

        /// <summary>
        /// 项目编号 实名制平台自动生成
        ///</summary>
        [Column("prj_num")]
        public string PrjNum { get; set; }

        /// <summary>
        /// 是否有施工许可证
        ///</summary>
        [Column("is_builder_licence")]
        public bool IsBuilderLicence { get; set; }

        /// <summary>
        /// 施工许可证编号
        ///</summary>
        [Column("builder_licences")]
        public string? BuilderLicences { get; set; }

        /// <summary>
        /// 施工许可证发证日期
        ///</summary>
        [Column("builder_licences_grant_date")]
        public DateTime? BuilderLicencesGrantDate { get; set; }

        /// <summary>
        /// 施工许可证有效期限
        ///</summary>
        [Column("builder_licences_term_date")]
        public DateTime? BuilderLicencesTermDate { get; set; }

        /// <summary>
        /// 项目简介
        ///</summary>
        [Column("description")]
        public string? Description { get; set; }

        /// <summary>
        /// 项目分类。参建项目分类字典表
        ///</summary>
        [Column("category")]
        public 项目分类? Category { get; set; }

        /// <summary>
        /// 建设单位Id
        ///</summary>
        [Column("build_corp_id")]
        public int BuildCorpId { get; set; }

        /// <summary>
        /// 建设单位名称
        ///</summary>
        [Column("build_corp_name")]
        public string BuildCorpName { get; set; }

        /// <summary>
        /// 建设单位统一社会信用代码，如果无统一社会信用代码，则填写组织机构代码
        ///</summary>
        [Column("build_corp_code")]
        public string BuildCorpCode { get; set; }

        /// <summary>
        /// 建设用地规划许可证编号。AES
        ///</summary>
        [Column("build_plan_num")]
        public string? BuildPlanNum { get; set; }

        /// <summary>
        /// 建设单位联系电话
        ///</summary>
        [Column("build_phone_num")]
        public string BuildPhoneNum { get; set; }

        /// <summary>
        /// 建设工程规划许可证编号。AES
        ///</summary>
        [Column("prj_plan_num")]
        public string? PrjPlanNum { get; set; }

        /// <summary>
        /// 项目所在城市。参考行政区划字典表
        ///</summary>
        [Column("city_code")]
        public int CityCode { get; set; }

        /// <summary>
        /// 项目所在区县。参考行政区划字典表
        ///</summary>
        [Column("area_code")]
        public int AreaCode { get; set; }

        /// <summary>
        /// 总投资，单位：（万元）
        ///</summary>
        [Column("invest")]
        public decimal? Invest { get; set; }

        /// <summary>
        /// 总面积，单位：平方米
        ///</summary>
        [Column("building_area")]
        public decimal? BuildingArea { get; set; }

        /// <summary>
        /// 总长度，单位：米
        ///</summary>
        [Column("building_length")]
        public decimal? BuildingLength { get; set; }

        /// <summary>
        /// 实际开工日期
        ///</summary>
        [Column("start_date")]
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 竣工日期，精确到 天，格式：yyyy-MM-dd
        ///</summary>
        [Column("end_date")]
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 联系人姓名
        ///</summary>
        [Column("link_man")]
        public string? LinkMan { get; set; }

        /// <summary>
        /// 联系人办公电话
        ///</summary>
        [Column("link_phone")]
        public string? LinkPhone { get; set; }

        /// <summary>
        /// 项目状态。参考项目状态字典表
        ///</summary>
        [Column("prj_status")]
        public 项目状态? PrjStatus { get; set; }

        /// <summary>
        /// 坐标数量
        ///</summary>
        [Column("position_count")]
        public int PositionCount { get; set; }

        ///// <summary>
        ///// WGS84 纬度
        /////</summary>
        //[Column("lat")]
        //public decimal Lat { get; set; }
        ///// <summary>
        ///// WGS84 经度
        /////</summary>
        //[Column("lng")]
        //public decimal Lng { get; set; }
        /// <summary>
        /// 项目地点
        ///</summary>
        [Column("address")]
        public string? Address { get; set; }

        /// <summary>
        /// 立项文号
        ///</summary>
        [Column("approval_num")]
        public string? ApprovalNum { get; set; }

        /// <summary>
        /// 立项级别。参考立项级别字典表
        ///</summary>
        [Column("approval_level")]
        public 项目立项级别? ApprovalLevel { get; set; }

        /// <summary>
        /// 建设规模。参考建设规模字典表
        ///</summary>
        [Column("size_type")]
        public 项目建设规模类型? SizeType { get; set; }

        /// <summary>
        /// 建设性质。参考建设性质分类字典表
        ///</summary>
        [Column("property_num")]
        public 项目建设性质分类? PropertyNum { get; set; }

        /// <summary>
        /// 工程用途。参考工程用途字典表
        ///</summary>
        [Column("function_num")]
        public 工程用途? FunctionNum { get; set; }

        /// <summary>
        /// 工程建设项目资金来源的种类，详见代码表
        ///</summary>
        [Column("funds_source")]
        public 项目资金来源? FundsSource { get; set; }

        /// <summary>
        /// 对应《工程建设项目审批管理系统》项目代码。（如果有）
        ///</summary>
        [Column("gg_project_code")]
        public string? GgProjectCode { get; set; }

        /// <summary>
        /// 对应《工程建设项目审批管理系统》工程代码。（如果有）
        ///</summary>
        [Column("gg_engineering_code")]
        public string? GgEngineeringCode { get; set; }

        /// <summary>
        /// 创建数据的项目管理员ID
        ///</summary>
        [Column("create_manager_id")]
        public int? CreateManagerId { get; set; }

        /// <summary>
        /// 当前管理员id(可修改)
        ///</summary>
        [Column("manager_id")]
        public int? ManagerId { get; set; }

        /// <summary>
        /// 审核状态
        ///</summary>
        [Column("audit_status")]
        public 项目审核状态? AuditStatus { get; set; }

        /// <summary>
        /// 审核原因
        /// </summary>
        [Column("audit_reason")]
        public string AuditReason { get; set; }

        /// <summary>
        /// 指工程建设项目中标合同价格，单位为元（不需要招投标的项目使用工程造价代替）
        ///</summary>
        [Column("contract_amount")]
        public decimal? ContractAmount { get; set; }

        /// <summary>
        /// 指工程建设项目合同约定的项目开工日期
        ///</summary>
        [Column("contract_start_date")]
        public DateTime ContractStartDate { get; set; }

        /// <summary>
        /// 指工程建设项目合同约定的项目完工日期
        ///</summary>
        [Column("contract_end_date")]
        public DateTime? ContractEndDate { get; set; }

        /// <summary>
        /// 是否树立维权告示牌
        /// 默认值: b'0'
        ///</summary>
        [Column("has_safeguarding_board")]
        public bool HasSafeguardingBoard { get; set; }

        /// <summary>
        /// 是否有工程款支付担保
        /// 默认值: b'0'
        ///</summary>
        [Column("has_earnest_money")]
        public bool? HasEarnestMoney { get; set; }

        /// <summary>
        /// 担保单位名称
        ///</summary>
        [Column("earnest_unit_name")]
        public string? EarnestUnitName { get; set; }

        /// <summary>
        /// 担保单位社会统一信用代码
        /// </summary>
        [Column("earnest_unit_code")]
        public string? EarnestUnitCode { get; set; }

        /// <summary>
        /// 担保金额
        ///</summary>
        [Column("earnest_money")]
        public decimal? EarnestMoney { get; set; }

        /// <summary>
        /// 担保开始日期
        ///</summary>
        [Column("earnest_start_date")]
        public DateTime? EarnestStartDate { get; set; }

        /// <summary>
        /// 担保终止日期
        ///</summary>
        [Column("earnest_end_date")]
        public DateTime? EarnestEndDate { get; set; }

        /// <summary>
        /// 创建时间
        ///</summary>
        [Column("create_time")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 是否已删除
        /// 默认值: b'0'
        ///</summary>
        [Column("is_deleted")]
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 总包企业id
        ///</summary>
        [Column("lump_contractor_corp_id")]
        public int LumpContractorCorpId { get; set; }

        /// <summary>
        /// 总包企业联系电话
        ///</summary>
        [Column("lump_contractor_corp_phone_num")]
        public string LumpContractorCorpPhoneNum { get; set; }

        /// <summary>
        /// 监理企业id
        ///</summary>
        [Column("supervisor_corp_id")]
        public int? SupervisorCorpId { get; set; }

        /// <summary>
        /// 序号（根据当前行政区划代码+当天自动生成）
        ///</summary>
        [Column("serial_number")]
        public int SerialNumber { get; set; }

        /// <summary>
        /// 是否多地施工
        /// </summary>
        [Column("is_multi_construction")]
        public bool IsMultiConstruction { get; set; }

        /// <summary>
        /// 监管单位Id
        /// </summary>
        [Column("dept_id")]
        public int DeptId { get; set; }

        /// <summary>
        /// 是否有监理企业
        /// </summary>
        [Column("has_supervisor")]
        public bool HasSupervisor { get; set; }

        /// <summary>
        /// 无监理企业原因
        /// </summary>
        [Column("supervisor_content")]
        public string? SupervisorContent { get; set; }

        /// <summary>
        /// 中标通知书编号
        /// </summary>
        [Column("letter_acceptance_num")]
        public string? LetterAcceptanceNum { get; set; }

        /// <summary>
        /// 招标标段编号
        /// </summary>
        [Column("bidding_section_num")]
        public string? BiddingSectionNum { get; set; }

        [Column("is_register")]
        public bool IsRegister { get; set; }

        /// <summary>
        /// 数据来源
        /// </summary>
        [Column("data_source")]
        public 项目数据来源? DataSource { get; set; }

        /// <summary>
        /// 是否需要完善数据（仅监管平台同步的数据该字段为true）
        /// </summary>
        [Column("need_complet_data")]
        public bool NeedCompletData { get; set; }

        /// <summary>
        /// 是否已同步（用于和第三方平台同步标识，同步的时候需要判断该标识）
        /// </summary>
        [Column("is_sync")]
        public bool IsSync { get; set; } = false;

        [Ignore]
        bool IDelEntity.IsDeletedInDatabase { get => IsDeleted; set => IsDeleted = value; }

        #region IEntity 成员

        [Ignore]
        object IEntity.EntityId { get { return this.ProjectId; } }

        #endregion IEntity 成员
    }
}